<?php
class DateTimeService extends ServiceBase
{
    public function compareWithCurrent($params=array())
    {
        $hour = self::getParam($params, 'hour', 0);
        $min = self::getParam($params, 'min', 0);
        $date = self::getParam($params, 'date', '');
        
        if (empty($date))
        {
            $this->result->fail(ServiceResult::RETURN_FAILURE, 'date is missing');
            return $this->result;
        }    
        
        $date = date('Y-m-d',strtotime($date));
        $currentDateTime = date('Y-m-d H:i');
        $arrCurrent = explode(' ',$currentDateTime);
        $currentDate = trim($arrCurrent[0]);
        $currentTime = trim($arrCurrent[1]);
        
        // compare date
        $arrDateParts = explode('-',$date);
        $year = intval($arrDateParts[0]);
        $arrCurDateParts = explode('-',$currentDate);
        $curYear = intval($arrCurDateParts[0]);
        // compare year
        if ($year > $curYear)
        {
            $this->result->ReturnedData['compareResult'] = 'greater';
            return $this->result;
        }
        elseif ($year < $curYear)
        {
            $this->result->ReturnedData['compareResult'] = 'smaller';
            return $this->result;
        }
        else
        {
            // compare month
            $month = intval($arrDateParts[1]);
            $curMonth = intval($arrCurDateParts[1]);
            if ($month > $curMonth)
            {
                $this->result->ReturnedData['compareResult'] = 'greater';
                return $this->result;
            }
            elseif ($month < $curMonth)
            {
                $this->result->ReturnedData['compareResult'] = 'smaller';
                return $this->result;
            }
            else
            {
                // compare day
                $day = intval($arrDateParts[2]);
                $curDay = intval($arrCurDateParts[2]);
                if ($day > $curDay)
                {
                    $this->result->ReturnedData['compareResult'] = 'greater';
                    return $this->result;
                }
                elseif ($day < $curDay)
                {
                    $this->result->ReturnedData['compareResult'] = 'smaller';
                    return $this->result;
                }
            }   
        }
        
        // if date is equal, compare time
        $arrCurTime = explode(':',$currentTime);
        $curHour = intval($arrCurTime[0]);
        $curMin = intval($arrCurTime[1]);
        if ($hour > $curHour)
        {
            $this->result->ReturnedData['compareResult'] = 'greater';
            return $this->result;    
        }
        elseif ($hour < $curHour)
        {
            $this->result->ReturnedData['compareResult'] = 'smaller';
            return $this->result;    
        }
        else
        {
            if ($min > $curMin)
            {
                $this->result->ReturnedData['compareResult'] = 'greater';
                return $this->result;    
            }
            elseif ($min < $curMin)
            {
                $this->result->ReturnedData['compareResult'] = 'smaller';
                return $this->result;    
            }    
        }
        
        $this->result->ReturnedData['compareResult'] = 'equal';
        return $this->result;
    }
    
    public function compareDateWithCurrent($params=array())
    {
        $date = self::getParam($params, 'date', '');
        
        if (empty($date))
        {
            $this->result->fail(ServiceResult::RETURN_FAILURE, 'date is missing');
            return $this->result;
        }    
        
        $timestamp = CDateTimeParser::parse($date,'dd/MM/yyyy');
        $date = date('Y-m-d',$timestamp);
        $currentDate = date('Y-m-d');
        
        // compare date
        $arrDateParts = explode('-',$date);
        $year = intval($arrDateParts[0]);
        $arrCurDateParts = explode('-',$currentDate);
        $curYear = intval($arrCurDateParts[0]);
        // compare year
        if ($year > $curYear)
        {
            $this->result->ReturnedData['compareResult'] = 'greater';
            return $this->result;
        }
        elseif ($year < $curYear)
        {
            $this->result->ReturnedData['compareResult'] = 'smaller';
            return $this->result;
        }
        else
        {
            // compare month
            $month = intval($arrDateParts[1]);
            $curMonth = intval($arrCurDateParts[1]);
            if ($month > $curMonth)
            {
                $this->result->ReturnedData['compareResult'] = 'greater';
                return $this->result;
            }
            elseif ($month < $curMonth)
            {
                $this->result->ReturnedData['compareResult'] = 'smaller';
                return $this->result;
            }
            else
            {
                // compare day
                $day = intval($arrDateParts[2]);
                $curDay = intval($arrCurDateParts[2]);
                if ($day > $curDay)
                {
                    $this->result->ReturnedData['compareResult'] = 'greater';
                    return $this->result;
                }
                elseif ($day < $curDay)
                {
                    $this->result->ReturnedData['compareResult'] = 'smaller';
                    return $this->result;
                }
            }   
        }
        
        $this->result->ReturnedData['compareResult'] = 'equal';
        return $this->result;
    }
}
?>